package com.dengdai.applibrary.activitys;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.dengdai.applibrary.BaseApplication;
import com.dengdai.applibrary.adapter.BaseCommAdapter;
import com.dengdai.applibrary.config.ResIdName;
import com.dengdai.applibrary.config.ViewInfo;
import com.dengdai.applibrary.interfaces.ListInterface;
import com.dengdai.applibrary.utils.BaseViewUtils;
import com.dengdai.applibrary.utils.NetWorkUtils;
import com.dengdai.applibrary.widgets.custom.LoadDataEmptyView;
import com.dengdai.pullrefresh.library.PullToRefreshBase;
import com.dengdai.pullrefresh.library.PullToRefreshListView;

/**
 * @说明 列表基础类
 * @作者 LY
 * @时间 16/5/18 17:44
 * @邮箱 2743569843@qq.com
 * @版权 Copyright(c) 2016 LY-版权所有
 * @备注
 */
public abstract class BaseListActivity<T> extends BaseActivity implements ListInterface {
    protected PullToRefreshListView pullRefreshListView;
    protected LoadDataEmptyView loadDataEmpty;
    protected BaseCommAdapter baseCommAdapter;
    private View headerView;
    private View footerView;
    protected int page = 0;

    @Override
    public int getLayoutResID() {
        return BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.LAYOUT, ViewInfo.BASE_LIST_ACTIVITY);
    }

    @Override
    public void initData() {
        pullRefreshListView = (PullToRefreshListView) findViewById(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.ID, ResIdName.PULLLISTVIEW));
        loadDataEmpty = (LoadDataEmptyView) findViewById(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.ID, ResIdName.LOADDATAEMPTY));
    }

    @Override
    public void business() {
        baseCommAdapter = getAdapter();
        pullRefreshListView.getRefreshableView().setAdapter(baseCommAdapter);
        page = getResources().getInteger(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.INTEGER, ResIdName.DEFAULT_PAGE));
        //设置加载模式
        if (getListViewMode() != null) {
            pullRefreshListView.setMode(getListViewMode());
        } else {
            pullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        }
        //设置头部布局
        headerView = getHeaderView();
        if (headerView != null) {
            pullRefreshListView.getRefreshableView().addHeaderView(headerView);
        }
        //设置底部布局
        footerView = getFooterView();
        if (footerView != null) {
            pullRefreshListView.getRefreshableView().addFooterView(footerView);
        }
        //设置刷新和加载
        pullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {//下拉刷新
                page = getResources().getInteger(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.INTEGER, ResIdName.DEFAULT_PAGE));
                baseCommAdapter.clear();
                loadingData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {//上拉加载
                loadingData();
            }
        });
        //列表点击事件
        pullRefreshListView.getRefreshableView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (headerView != null) {
                    position = position - 2;
                } else {
                    position = position - 1;
                }
                itemClick(baseCommAdapter.getListData().get(position), position);
            }
        });

        {//加载数据
            loadDataEmpty.setVisibility(View.VISIBLE);
            if (NetWorkUtils.getNetworkStatus(BaseApplication.getAppContext())) {
                loadDataEmpty.loadingData();
                loadingData();
            } else {
                loadDataEmpty.netWorkError();
            }
        }
    }

    @Override
    public void stopRefreshView() {
        pullRefreshListView.onRefreshComplete();
        if (baseCommAdapter.getCount() > 0) {
            loadDataEmpty.setVisibility(View.GONE);
        } else {
            if (headerView != null) {
                loadDataEmpty.setVisibility(View.GONE);
            } else {
                loadDataEmpty.setVisibility(View.VISIBLE);
                loadDataEmpty.loadDataError();
            }
        }
    }
}
